import http from "@/api";
import { ResPage, User, OrgManager } from "@/api/interface/index";
import qs from "qs";
import { ElMessage } from "element-plus";
/**
 * @name 数据字典模块
 */

/**
 * @name 查询数据字典分组
 */
// 查询数据字典分组
export const dataDictionaryGroupFind = (params: {}, header: string, unhandle: string[]) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      if (unhandle.length > 0) {
        if (unhandle.includes(`${key}`)) {
          newobj[`${key}`] = `${params[key]}`;
        } else {
          newobj[header + "." + `${key}`] = `${params[key]}`;
        }
      } else {
        newobj[header + "." + `${key}`] = `${params[key]}`;
      }
    });
  } else {
    newobj = params;
  }
  // return http.post<ResPage<User.ResUserList>>("" + `/pages/itsopUser!findITSOPUserPager.action`, {}, { params });
  return http.postitsm<string>(`/pages/dataDictionaryGroup!find.action`, qs.stringify(newobj));
};

// 保存组
export const saveGroup = (params: {}, header: string) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      newobj[header + "." + `${key}`] = `${params[key]}`;
    });
  }
  return http.post<String>(`/pages/dataDictionaryGroup!saveGroup.action`, qs.stringify(newobj)); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
};

// 编辑组
export const mergeGroup = (params: {}, header: string) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      newobj[header + "." + `${key}`] = `${params[key]}`;
    });
  }
  return http.post<String>(`/pages/dataDictionaryGroup!mergeGroup.action`, qs.stringify(newobj)); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
};

// 批量删除组
export const deleteGroup = (params: string) => {
  return http.post<String>(`/pages/dataDictionaryGroup!removeGroup.action` + params, qs.stringify({})); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
};

// 导出组数据
export const exportDataDictionaryGroup = (params: OrgManager.ReqOrgManagerParams) => {
  let header = "reDto";
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      newobj[header + "." + `${key}`] = `${params[key]}`;
    });
  }
  return http.downloaditsm(`/pages/dataDictionaryGroup!exportDataDictionaryGroup.action`, qs.stringify(newobj));
};

// 查询项目
export const dataDictionaryItemsFind = (params: {}, header: string, unhandle: string[]) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      if (unhandle.length > 0) {
        if (unhandle.includes(`${key}`)) {
          newobj[`${key}`] = `${params[key]}`;
        } else {
          newobj[header + "." + `${key}`] = `${params[key]}`;
        }
      } else {
        newobj[header + "." + `${key}`] = `${params[key]}`;
      }
    });
  } else {
    newobj = params;
  }
  // return http.post<ResPage<User.ResUserList>>("" + `/pages/itsopUser!findITSOPUserPager.action`, {}, { params });
  return http.postitsm<string>(`/pages/dataDictionaryItems!find.action`, qs.stringify(newobj));
};

// 数据分类名称是否存在
export const isDataDictionaryItemsDnameExisted = (params: {}, header: string) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      newobj[header + "." + `${key}`] = `${params[key]}`;
    });
  }
  return http.post<String>(`/pages/dataDictionaryItems!isDataDictionaryItemsDnameExisted.action`, qs.stringify(newobj)); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
};

// 新增数据项目
export const dataDictionaryItems = (params: {}, header: string) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      newobj[header + "." + `${key}`] = `${params[key]}`;
    });
  }
  return http.post<String>(`/pages/dataDictionaryItems!save.action`, qs.stringify(newobj)); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
};

// 编辑项目
export const mergeDataDictionaryItems = (params: {}, header: string) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      newobj[header + "." + `${key}`] = `${params[key]}`;
    });
  }
  return http.post<String>(`/pages/dataDictionaryItems!merge.action`, qs.stringify(newobj)); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
};

// 导出dataitem数据
export const exportDictionaryItemData = (params: OrgManager.ReqOrgManagerParams) => {
  let header = "dataDictionaryQueryDto";
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      newobj[header + "." + `${key}`] = `${params[key]}`;
    });
  }
  return http.downloaditsm(`/pages/dataDictionaryItems!exportDictionaryItemData.action`, qs.stringify(newobj));
};

// 是否为系统配置数据
export const findByDcodeIsSystemData = (params: string) => {
  return http.post<String>(`/pages/dataDictionaryItems!findByDcodeIsSystemData.action` + params, qs.stringify({})); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
};

// 删除dataitem
export const deleteDataDictionaryItems = (params: string) => {
  return http.post<String>(`/pages/dataDictionaryItems!delete.action` + params, qs.stringify({})); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
};

// 数据字典模板
export const tempDataDictionary = (params: OrgManager.ReqOrgManagerParams) => {
  return http.download(`importFile/DataDictionary.zip`, params);
};

// 数据字典导入
export const importDataDictionaryItemData = (params: FormData) => {
  return http.post(`/pages/dataDictionaryItems!importDataDictionaryItemData.action`, params);
};

export const getDataDictionaryGroup = params => {
  // console.log(params);
  return http.post(
    "/pages/dataDictionaryGroup!find.action",
    qs.stringify({
      page: params.pageNum,
      rows: params.pageSize
    })
  );
};
//删除数据字典
export const deleteDataDictionaryGroup = params => {
  return http.post("/pages/dataDictionaryGroup!removeGroup.action", params);
};
//获取影响范围紧急度矩阵
export const getPriorityMatrix = () => {
  return http.post("/pages/priorityMatrix!findPriorityMatrixDataShow.action");
};
//获取优先级
export const getPriority = () => {
  return http.post("/pages/dataDictionaryItems!findByCode.action?dataDictionaryQueryDto.groupCode=priority");
};
//获取紧急度
export const getUrgency = () => {
  return http.post("/pages/dataDictionaryItems!findByCode.action?dataDictionaryQueryDto.groupCode=seriousness");
};
//获取影响范围
export const getImpact = () => {
  return http.post("/pages/dataDictionaryItems!findByCode.action?dataDictionaryQueryDto.groupCode=effectRange");
};
//获取流程
export const getProcess = () => {
  return http.post("/pages/jbpm!findPageProcessDefinitions.action?rows=10000");
};
//获取请求分类
export const getRequestCategory = () => {
  return http.post("/pages/clientRequest!getCategoryTreeClientnew.action");
};
//提交优先级
export const submitPriority = params => {
  return http.post("/pages/priorityMatrix!savePriorityMatrix.action", qs.stringify(params));
};
//检查覆盖优先级矩阵状态
export const checkPriorityMatrixStatus = () => {
  return http.post("/pages/priorityMatrix!findPriorityMatrixStatus.action");
};
//覆盖优先级矩阵
export const priorityMatrixStatus = params => {
  return http.post("/pages/priorityMatrix!savePriorityMatrixStatus.action", qs.stringify(params));
};
//获取自定义分组列表
export const getCustomGroupList = params => {
  return http.post(
    "/pages/attrGroup!find.action?attrGroupQueryDTO.attrGroupId=0",
    qs.stringify({
      page: params.pageNum,
      rows: params.pageSize,
      "attrGroupQueryDTO.attrGroupName": params["attrGroupQueryDTO.attrGroupName"]
    })
  );
};
//添加自定义分组字段
export const addCustomGroup = params => {
  return http.post("/pages/attrGroup!saveAttrGroup.action", qs.stringify(params));
};
//删除自定义分组字段
export const deleteCustomGroup = params => {
  return http.post("/pages/attrGroup!deleteAttrGroup.action", params);
};
//编辑自定义分组字段
export const editCustomGroup = params => {
  return http.post("/pages/attrGroup!updateAttrGroup.action", qs.stringify(params));
};
//获取自定义类型列表
export const getCustomTypeList = params => {
  // console.log(params);
  return http.post(
    "/pages/eav!findEavEntity.action?sidx=eavNo&sord=desc",
    qs.stringify({
      page: params.pageNum,
      rows: params.pageSize,
      "eavEntityDTO.eavName": params["eavEntityDTO.eavName"],
      "eavEntityDTO.desc": "系统数据"
    })
  );
};

//获取自定义类型列表
export const getCustomTypeListReport = params => {
  // console.log(params);
  return http.post(
    "/pages/eav!findEavEntityReport.action?sidx=eavNo&sord=desc",
    qs.stringify({
      page: params.pageNum,
      rows: params.pageSize,
      "eavEntityDTO.eavName": params["eavEntityDTO.eavName"],
      "eavEntityDTO.desc": "系统数据"
    })
  );
};
//获取继承类型列表
export const getParentCustomTypeList = params => {
  return http.post("/pages/eav!findEavEntity.action", qs.stringify(params));
};
//添加自定义类型
export const addCustomType = params => {
  return http.post("/pages/eav!saveEavEntity.action", qs.stringify(params));
};
//编辑自定义类型
export const editCustomType = params => {
  return http.post("/pages/eav!updateEavEntity.action", qs.stringify(params));
};
//删除自定义类型
export const deleteCustomType = params => {
  return http.post("/pages/eav!deleteEavEntity.action", params);
};
//获取自定义字段列表
export const getCustomFieldList = params => {
  // console.log(params);
  if (params["attrsQueryDTO.eavNo"] === "" || params["attrsQueryDTO.eavNo"] === undefined) {
    ElMessage.error("请选择自定义类型");
    return Promise.reject(new Error("请选择自定义类型"));
  } else {
    return http.post(
      "/pages/attrs!find.action?attrsQueryDTO.eavNo=" + params["attrsQueryDTO.eavNo"],
      qs.stringify({
        page: params.pageNum,
        rows: params.pageSize,
        sidx: "sortNo",
        sord: "desc",
        "attrsQueryDTO.attrAsName": params["attrsQueryDTO.attrAsName"]
      })
    );
  }
};
//获取自定义类型所属组
export const getCustomGroupList_addType = () => {
  return http.post(
    "/pages/attrGroup!find.action?attrGroupQueryDTO.attrGroupId=0",
    qs.stringify({
      _search: false,
      rows: 1000,
      page: 1,
      sidx: "attrGroupSortNo",
      sord: "desc"
    })
  );
};
//获取数据字典组
export const getDataDictionaryGroup_addType = () => {
  return http.post("/pages/dataDictionaryGroup!findDataDictionary.action");
};
//添加自定义字段
export const addCustomField = params => {
  return http.post("/pages/attrs!save.action", qs.stringify(params));
};
//编辑自定义字段
export const editCustomField = params => {
  return http.post("/pages/attrs!update.action", qs.stringify(params));
};
//删除自定义字段
export const deleteCustomField = params => {
  return http.post("/pages/attrs!delete.action", params);
};
//获取回访事项列表
export const getVisitList = params => {
  return http.post(
    "/pages/visit!visitFindPager.action",
    qs.stringify({
      page: params.pageNum,
      rows: params.pageSize,
      _search: false,
      sidx: "visitNo",
      sord: "desc",
      "visitDTO.visitName": params["visitDTO.visitName"]
    })
  );
};
//添加回访事项
export const addVisitList = params => {
  return http.post("/pages/visit!visitSave.action", qs.stringify(params));
};
//修改回访事项
export const editVisitList = params => {
  return http.post("/pages/visit!visitUpdate.action", qs.stringify(params));
};
//删除回访事项
export const deleteVisitList = params => {
  return http.post("/pages/visit!visitDelete.action", params);
};
//获取回访选项值列表
export const getVisitItem = params => {
  console.log(params);
  return http.post(
    "/pages/visit!visitItemFindPager.action?visitNo=" + params.visitNo,
    qs.stringify({
      page: params.pageNum,
      rows: params.pageSize,
      _search: false,
      sidx: "visitItemNo",
      sord: "desc"
    })
  );
};
//添加回访选项值
export const addVisitItemList = params => {
  return http.post("/pages/visit!visitItemSave.action", qs.stringify(params));
};
//编辑回访选项值
export const editVisitItemList = params => {
  return http.post("/pages/visit!visitItemUpdate.action", qs.stringify(params));
};
//删除回访选项值
export const deleteVisitItemList = params => {
  return http.post("/pages/visit!visitItemDelete.action", params);
};
//获取升级级别列表
export const getUpgradeLevelList = params => {
  return http.post(
    "/pages/updatelevel!findAllLevels.action",
    qs.stringify({
      page: params.pageNum,
      rows: params.pageSize,
      _search: false,
      sidx: "ulId",
      sord: "desc",
      "updateLevelQueryDTO.ulName": params["updateLevelQueryDTO.ulName"]
    })
  );
};
//添加前验证是否存在
export const checkUpgradeLevel = params => {
  return http.post("/pages/updatelevel!isUpgradeLevelNameExisted.action", qs.stringify(params));
};
//添加升级级别
export const addUpgradeLevel = params => {
  return http.post("/pages/updatelevel!save.action", qs.stringify(params));
};
//提交编辑前验证是否存在
export const checkUpgradeLevelEdit = params => {
  return http.post("/pages/updatelevel!isUpgradeLevelNameExistedOnEdit.action", qs.stringify(params));
};
//编辑升级级别
export const editUpgradeLevel = params => {
  return http.post("/pages/updatelevel!merge.action", qs.stringify(params));
};
//删除升级级别
export const delUpdateLevel = params => {
  return http.post("/pages/updatelevel!delete.action", params);
};
//获取变更委员会列表
export const getChangeCommitteeList = params => {
  return http.post(
    "/pages/cab!findPagerCAB.action",
    qs.stringify({
      page: params.pageNum,
      rows: params.pageSize,
      _search: false,
      sidx: "cabId",
      sord: "desc",
      "cabQueryDto.cabName": params["cabQueryDto.cabName"]
    })
  );
};
//添加变更委员会前验证
export const checkChangeCommittee = params => {
  return http.post("/pages/cab!isCabNameExisted.action", qs.stringify(params));
};
//添加变更委员会
export const addChangeCommittee = params => {
  return http.post("/pages/cab!saveCAB.action", qs.stringify(params));
};
//编辑变更委员会前验证
export const checkChangeCommitteeEdit = params => {
  return http.post("/pages/cab!isCabNameExistedOnEdit.action", qs.stringify(params));
};
//编辑变更委员会
export const editChangeCommittee = params => {
  return http.post("/pages/cab!editCAB.action", qs.stringify(params));
};
//删除变更委员会
export const delChangeCommittee = params => {
  return http.post("/pages/cab!deleteCAB.action", params);
};
//获取委员会成员列表
export const getChangeCommitteeMemberList = params => {
  return http.post(
    "/pages/cab!findCABMember.action?cabId=" + params.cabId,
    qs.stringify({
      page: params.pageNum,
      rows: params.pageSize,
      _search: false,
      sidx: "cabMemberId",
      sord: "desc"
    })
  );
};
//添加变更委员会成员
export const addChangeCommitteeMember = params => {
  return http.post("/pages/cabMember!addCabMember.action", qs.stringify(params));
};
//编辑变更委员会成员
export const editChangeCommitteeMember = params => {
  return http.post("/pages/cabMember!editCabMember.action", qs.stringify(params));
};
//删除变更委员会成员
export const delChangeCommitteeMember = params => {
  return http.post("/pages/cabMember!deleteCABMember.action", params);
};
// 通过编号查询数据字典item
export const dataDictionaryItemsfindByCode = (params: string) => {
  let timestr = Date.now() + Math.random();
  return http.post<String>(
    `/pages/dataDictionaryItems!findByCode.action?dataDictionaryQueryDto.groupCode=` + params + `&time=` + timestr,
    qs.stringify({})
  ); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
};

// 获取分类树
export const getCategoryTreeAllData = (params: string) => {
  return http.post(
    `/pages/event!getCategoryTreeAllData.action?flag=dataDic&num=50&parentEventId=0&types=` + params,
    qs.stringify({})
  ); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
};

// 分页查询ci列表
export const cItemsFind = (params: {}, header: string, unhandle: string[]) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      if (unhandle.length > 0) {
        if (unhandle.includes(`${key}`)) {
          newobj[`${key}`] = `${params[key]}`;
        } else {
          newobj[header + "." + `${key}`] = `${params[key]}`;
        }
      } else {
        newobj[header + "." + `${key}`] = `${params[key]}`;
      }
    });
  } else {
    newobj = params;
  }
  return http.post(`/pages/ci!cItemsFind.action`, qs.stringify(newobj));
};
